using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.IO;
using System.Deployment.Application;

namespace GDIdotNETgames
{
    public partial class FormMain : Form
    {
        private string _filename = string.Empty;

        public FormMain()
        {
            InitializeComponent();
            Open(AppDomain.CurrentDomain.BaseDirectory + "Content/XMLFileGame.xml");
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAboutDialog _aboutBox = new FormAboutDialog();
            if (ApplicationDeployment.IsNetworkDeployed)
                _aboutBox.Version = "ver " + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
            else
                _aboutBox.Version = "(not network deployed)";
            _aboutBox.RootPathname = AppDomain.CurrentDomain.BaseDirectory;
            _aboutBox.ShowDialog();
            _aboutBox.Dispose();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Open(AppDomain.CurrentDomain.BaseDirectory + "Content/XMLFileGame.xml");
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialogGame.ShowDialog() == DialogResult.OK)
            {
                Open(openFileDialogGame.FileName);
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialogGame.ShowDialog() == DialogResult.OK)
            {
                Save(saveFileDialogGame.FileName);
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_filename.Equals(string.Empty))
            {
                if (saveFileDialogGame.ShowDialog() == DialogResult.OK)
                {
                    Save(saveFileDialogGame.FileName);
                }
            }
            else
                Save(_filename);
        }

        private void Open(string filename)
        {
            using (XmlReader reader = XmlReader.Create(filename))
            {
                reader.ReadToDescendant("Game");
                Text = reader.GetAttribute("Title");
                userControlMapGame.LoadFromXML(reader.ReadSubtree());
            }
            _filename = filename;
        }

        private void Save(string filename)
        {
            using (XmlTextWriter writer = new XmlTextWriter(filename, null))
            {
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                writer.WriteStartElement("Game");
                writer.WriteAttributeString("Title", Text);
                userControlMapGame.SerializeToXML(writer);
                writer.WriteEndDocument();
            }
            _filename = filename;
        }
    }
}